package com.stylefeng.guns.modular.utils;

import com.stylefeng.guns.common.persistence.model.ProjectTable;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author lmshe
 * @create 2018-07-09 14:30
 * @desc 修改相关文件的配置
 */
public class ModifyConfig {
    //替换关键字
    public static final String MODIFY_KEY = "//{modify-this-file}";

    //修改AutofacWebConfig
    public static final String AUCTOFACWEB_CONFIG_PATH = "E:\\myself\\project\\park_management\\SP.Web\\App_Start\\AutofacWebConfig.cs";

    //修改DBContext
    public static final String IAPP_DB_CONTEXT_PATH = "E:\\myself\\project\\park_management\\SP.Data\\IAppDbContext.cs";

    //修改DBContext
    public static final String APP_DB_CONTEXT_PATH = "E:\\myself\\project\\park_management\\SP.Data\\AppDbContext.cs";

    //修改Configuration
    public static final String CONFIGURATION_PATH = "E:\\myself\\project\\park_management\\SP.Data\\Migrations\\Configuration.cs";


    /**
     * 修改配置
     * @param path
     * @param changeContent
     */
    public static void modifyConfig(String path,String changeContent,String tableName,int sort){
        try {
            File file = new File(path);
            String fileContent = FileUtils.readFileToString(file,"UTF-8");
            byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
            String res = new String(uft8bom,"UTF-8");
            if(!fileContent.contains(res)){
                fileContent = res + fileContent;
            }
            switch (sort){
                case 1:
                    if(!fileContent.contains("builder.RegisterType<"+GeneratorUtils.initcap(tableName)+"Service>()")){
                        fileContent = fileContent.replace(MODIFY_KEY,changeContent);
                        FileUtils.writeStringToFile(file, fileContent,"UTF-8");
                    }
                    break;
                case 2:
                    if(!fileContent.contains("DbSet<"+GeneratorUtils.initcap(tableName)+">")){
                        fileContent = fileContent.replace(MODIFY_KEY,changeContent);
                        FileUtils.writeStringToFile(file, fileContent,"UTF-8");
                    }
                    break;
                case 3:
                    if(!fileContent.contains("DbSet<"+GeneratorUtils.initcap(tableName)+">")){
                        fileContent = fileContent.replace(MODIFY_KEY,changeContent);
                        FileUtils.writeStringToFile(file, fileContent,"UTF-8");
                    }
                    break;
                case 4:
                    if(!fileContent.contains("\""+GeneratorUtils.initcap(tableName)+"Page\"")){
                        fileContent = fileContent.replace(MODIFY_KEY,changeContent);
                        FileUtils.writeStringToFile(file, fileContent,"UTF-8");
                    }
                    break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改相关配置文件
     * @param projectTable
     */
    public static void modifyConfigs(ProjectTable projectTable) {
        String tableName = projectTable.getTableName();
        //修改AutofacWebConfig
        String changeContentA = "builder.RegisterType<"+ GeneratorUtils.initcap(tableName) +"Service>().As<I"+GeneratorUtils.initcap(tableName)+"Service>().InstancePerRequest();\r\n";
        changeContentA = changeContentA + "            " + MODIFY_KEY;
        modifyConfig(AUCTOFACWEB_CONFIG_PATH,changeContentA,tableName,1);
        //修改DBContext
        String changeContentB = "DbSet<"+ GeneratorUtils.initcap(tableName) +"> "+ GeneratorUtils.initcap(tableName) +"s { get; set; }\r\n";
        changeContentB = changeContentB + "        " + MODIFY_KEY;
        modifyConfig(IAPP_DB_CONTEXT_PATH,changeContentB,tableName,2);
        //修改DBContext
        String changeContentC = "public virtual DbSet<"+ GeneratorUtils.initcap(tableName) +"> "+ GeneratorUtils.initcap(tableName) +"s { get; set; }\r\n";
        changeContentC = changeContentC + "        " + MODIFY_KEY;
        modifyConfig(APP_DB_CONTEXT_PATH,changeContentC,tableName,3);
        //修改Configuration
        StringBuffer sb = new StringBuffer();
        sb.append("#region "+projectTable.getTableDesc()+"\r\n");
        sb.append("            context.Permissions.AddOrUpdate(p => p.Tag,\r\n");
        sb.append("                 new Permission() { Tag = \""+ GeneratorUtils.initcap(tableName) +"Page\", Name = \""+projectTable.getTableDesc()+"\", Category = (int)PermissionCategory.Menu, NavigateUrl = \"/"+GeneratorUtils.initcap(tableName)+"/index\",\r\n");
        sb.append("                 Sort = 12, ParentId = null, IsUsed = true, CreatedBy = \"Admin\", CreatedTime = DateTime.Now, UpdatedBy = \"Admin\",\r\n");
        sb.append("                 UpdatedTime = DateTime.Now, IsDeleted = false, IconFont = \"iconfont icon-shequyunying\" }\r\n");
        sb.append("                 );\r\n");
        sb.append("            context.SaveChanges();\r\n");
        sb.append("            #endregion\r\n");
        sb.append("            " + MODIFY_KEY);
        modifyConfig(CONFIGURATION_PATH,sb.toString(),tableName,4);
    }
    public static void main(String[] args) {
        ProjectTable projectTable = new ProjectTable();
        projectTable.setTableName("documents");
        projectTable.setTableDesc("文档管理");
        modifyConfigs(projectTable);
    }
}
